<?php

namespace App\Model;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ContentCheck extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'content_check';
    protected $user_table = 'user';
    protected $room_table = 'room';
    protected $table_log = 'content_check_log';


    public function getFirstContentList($input = [])
    {
        $limit = $input['limit'] ?? 10;
        $where['check_obj'] = $input['type'] ?? 1;
        $where['status'] = $input['status'] ?? 0;
        $where['is_delete'] = 0;
        $res = '';
        if (in_array($where['check_obj'], [1, 2, 6, 8, 9, 11, 12 ,24,25,26])) {
            if (!empty($input['uuid'])) {
                $where['uuid'] = $input['uuid'];
            }
            $sql = DB::connection($this->connection)->table($this->table)
                ->where($where);
            if ($where['check_obj'] == 8) {
                $sql = $sql->where('content','!=' ,'');
            }
            $res = $sql->leftJoin('user', 'user.user_id', '=', $this->table . '.user_id')
                ->orderBy('created_at', 'asc')
                ->select($this->table . '.*', 'uuid')
                ->paginate($limit);
        } else if (in_array($where['check_obj'], [3, 4, 5, 7.0, 7.1, 13])) {
            //单独处理是小屋还是CBD
            if($where['check_obj'] == 7.0 || $where['check_obj'] == 7.1) {
                $fastVal = explode('.', $where['check_obj']);
                $where['check_obj'] = $fastVal[0];
                $where['business_type'] = $fastVal[1];
            }
            if (!empty($input['uuid'])) {
                $where['room_uid'] = $input['uuid'];
            }
            $res = DB::connection($this->connection)->table($this->table)
                ->where($where)
                ->leftJoin('room', 'room.room_id', '=', $this->table . '.data_id')
                ->orderBy('created_at', 'asc')
                ->select($this->table . '.*', 'room_uid as uuid')
                ->paginate($limit);
        }else if(in_array($where['check_obj'], [14, 15,17])){
            if (!empty($input['uuid'])) {
                $where['uuid'] = $input['uuid'];
            }
            $res = DB::connection($this->connection)->table($this->table)
                ->where($where)
                ->leftJoin('union', 'union.id', '=', $this->table . '.data_id')
                ->orderBy('created_at', 'asc')
                ->select($this->table . '.*','union.uuid as uuid')
                ->paginate($limit);
        }else if($where['check_obj'] == 16){
            if (!empty($input['uuid'])) {
                $where['union.uuid'] = $input['uuid'];
            }
            $res = DB::connection($this->connection)->table($this->table)
                ->where($where)
                ->leftJoin('union_notice', 'union_notice.id', '=', $this->table . '.content_id')
                ->leftJoin('union', 'union.id', '=', $this->table . '.data_id')
                ->orderBy('created_at', 'asc')
                ->select($this->table . '.*','union.uuid as uuid')
                ->paginate($limit);
        }else if(in_array($where['check_obj'], [18, 19 ,20])){
            if (!empty($input['uuid'])) {
                $where['uuid'] = $input['uuid'];
            }
            $res = DB::connection($this->connection)->table($this->table)
                ->where($where)
                ->leftJoin('family', 'family.id', '=', $this->table . '.data_id')
                ->orderBy('created_at', 'asc')
                ->select($this->table . '.*','family.uuid as uuid')
                ->paginate($limit);
        }else if($where['check_obj'] == 22){
            if (!empty($input['uuid'])) {
                $where['uuid'] = $input['uuid'];
            }
            unset($where['status']);
            $where[$this->table.'.status'] = $input['status'] ?? 0;
            $res = DB::connection($this->connection)->table($this->table)
                ->where($where)
                ->leftJoin('user', 'user.user_id', '=', $this->table . '.user_id')
                ->leftJoin('twitter_comment', 'comment_id', '=', $this->table . '.data_id')
                ->orderBy('created_at', 'asc')
                ->select($this->table . '.*','uuid' ,'twitter_comment.comment_text as comment_text','twitter_comment.comment_img as comment_img')
                ->paginate($limit);
        }else if($where['check_obj'] == 23){
            if (!empty($input['uuid'])) {
                $where['uuid'] = $input['uuid'];
            }
            unset($where['status']);
            $where[$this->table.'.status'] = $input['status'] ?? 0;
            $res = DB::connection($this->connection)->table($this->table)
                ->where($where)
                ->leftJoin('user', 'user.user_id', '=', $this->table . '.user_id')
                ->leftJoin('twitter_reply', 'twitter_reply.id', '=', $this->table . '.data_id')
                ->orderBy('created_at', 'asc')
                ->select($this->table . '.*','uuid' ,'twitter_reply.reply_content as reply_content','twitter_reply.reply_img as reply_img')
                ->paginate($limit);
        }
        $res = $this->objToArray($res);

        foreach ($res['data'] as &$v) {
            if ($where['check_obj'] == 9) {
                $content = json_decode($v['content'], true);
                $v['content'] = '标题《' . $content['title'] . '》——内容：' . $content['content'];
            }
            if ($where['check_obj'] == 11 || $where['check_obj'] == 12) {
                $content = json_decode($v['content'], true);
                $v['text_content'] = $content['comment_text'];
                $v['voice_content'] = $content['comment_voice'];
            }
            if($where['check_obj'] == 22){
                $content = json_decode($v['content'], true);
                $v['text_content'] = $content['comment_text'];
                if(!empty($v['comment_img'])){
                    $images = explode(',',$v['comment_img']);
                    foreach ($images as &$a){
                        $a = build_image($a);
                    }
                    $v['images'] = $images;
                }

            }
            if($where['check_obj'] == 23){
                $content = json_decode($v['content'], true);
                $v['text_content'] = $content['comment_text'];
                if(!empty($v['reply_img'])){
                    $images = explode(',',$v['reply_img']);
                    foreach ($images as &$a){
                        $a = build_image($a);
                    }
                    $v['images'] = $images;
                }
            }
            if($where['check_obj'] == 24){
                $content = json_decode($v['content'], true);
                $v['voice_content'] = $content['works_voice'];
            }
            if($where['check_obj'] == 25){
                $content = json_decode($v['content'], true);
                $v['text_content'] = $content['works_txt'];
                $v['text_title'] = $content['works_title'];
            }
            $v['created_at'] = date("Y-m-d H:i:s", $v['created_at']);
        }
        return $res;
    }

    public function getSecondContentList($input = [])
    {
        $page = $input['page'] ?? 1;
        $limit = $input['limit'] ?? 10;
        $check_obj = $input['type'] ?? 1;
        $status = $input['status'] ?? 1;//初审
        $check_type = $input['status'] ?? 1;//初审
        $sql_page = ($page - 1) * $limit;
        $time_out = empty($input['time_out']) ? 0 : $input['time_out'] * 60;

        $where = " `status` = $status";
        //判断是小屋还是CBD
        if($check_obj == 7.0 || $check_obj == 7.1) {
            $fastVal = explode('.', $check_obj);
            $check_obj = $fastVal[0];
            $where .= ' AND zb_room.business_type = '.$fastVal[1];
        }

        $where .= " AND `check_obj` = $check_obj and `is_delete` = 0 ";
        if ($status == 1 && $input['check_status'] != 0) {
            $where .= " AND zb_content_check.check_status = " . $input['check_status'];
        } else if ($status == 2 ) {
            if($input['check_status'] != 0){
                $where .= " AND zb_content_check.check_status = " . $input['check_status'];
            }
            if($input['second_status'] != 0){
                $where .= " AND review_check_status = " . $input['second_status'];
            }
        }
        if (!empty($input['opt_id'])) {
            $where = ' AND opt_id = ' . $input['opt_id'];
        }
        $having ='';
        if ($check_type == 2){
            $having = " having diff_t >= $time_out ";
        }
        $sql = '';
        if (in_array($check_obj, [1, 2, 6, 8, 9, 11, 12])) {
            if (!empty($input['uuid'])) {
                $where .= ' AND uuid = ' . $input['uuid'];
            }
            if($check_obj == 8){
                $where .= ' AND content != \'\'';
            }
            $aw[] = ['f.created_at', '<', 'created_at + 60'];
            $sql = "select `zb_content_check`.*, `zb_s`.`check_type`, `uuid`, `zb_s`.`opt_id` as 'first_user',`zb_f`.`opt_id` as 'second_user', `zb_f`.`created_at` as `f_created_at` , (zb_f.created_at - `zb_content_check`.created_at) as diff_t ,`zb_s`.`created_at` as `first_time`,`zb_f`.`created_at` as `second_time` from `zb_content_check`
                        left join `zb_user` on `zb_user`.`user_id` = `zb_content_check`.`user_id`
                        left join ( SELECT * FROM `zb_content_check_log` WHERE check_type = 1) as `zb_s` on `zb_s`.`content_check_id` = `zb_content_check`.`id`
                        left join ( SELECT * FROM `zb_content_check_log` WHERE check_type = 2) as `zb_f` on `zb_f`.`content_check_id` = `zb_content_check`.`id`
                        where ($where)
                        $having order by `zb_content_check`.`created_at` asc";
//            var_dump($sql);
        } else if (in_array($check_obj, [3, 4, 5, 7 ,13])) {
            if (!empty($input['uuid'])) {
                $where .= ' AND room_uid = ' . $input['uuid'];
            }

            $sql = "select `zb_content_check`.*, `room_uid` as 'uuid', `zb_s`.`opt_id` as 'first_user',`zb_f`.`opt_id` as 'second_user', `zb_f`.`created_at` as `f_created_at` , (zb_f.created_at - `zb_content_check`.created_at) as diff_t ,`zb_s`.`created_at` as `first_time`,`zb_f`.`created_at` as `second_time` from `zb_content_check`
                        left join `zb_room` on `zb_room`.`room_id` = `zb_content_check`.`data_id`
                        left join ( SELECT * FROM `zb_content_check_log` WHERE check_type = 1) as `zb_s` on `zb_s`.`content_check_id` = `zb_content_check`.`id`
                        left join ( SELECT * FROM `zb_content_check_log` WHERE check_type = 2) as `zb_f` on `zb_f`.`content_check_id` = `zb_content_check`.`id`
                        where ($where)
                        $having order by `zb_content_check`.`created_at` asc";
//            var_dump($sql);
        }
        $res = $this->objToArray(DB::connection($this->connection)->table(Db::raw("($sql) as aaa"))->paginate($limit));
        $admin_user = $this->objToArray(DB::table('users')->get());
        $admin_user_name = array_column($admin_user, 'name', 'id');
        foreach ($res['data'] as &$v) {
            if ($status == 2) {
//                $first_info = $this->objToArray(DB::connection($this->connection)->table($this->table_log)->where(['content_check_id' => $v['id'], 'check_type' => 1])->first());
                $v['first_opt_name'] = $v['first_user']?$admin_user_name[$v['first_user']]:'';
                $v['second_opt_name'] = $v['first_user']?$admin_user_name[$v['second_user']]:'';
            } else {
                $v['first_opt_name'] = $v['first_user']?$admin_user_name[$v['first_user']]:"";
                $v['second_opt_name'] = '暂无';
            }
            if ($check_obj == 9) {
                $content = json_decode($v['content'], true);
                $v['content'] = '标题《' . $content['title'] . '》——内容：' . $content['content'];
            }
            if ($check_obj == 11 || $check_obj == 12) {
                $content = json_decode($v['content'], true);
                $v['text_content'] = $content['comment_text'];
                $v['voice_content'] = $content['comment_voice'];
            }
            $v['first_date'] = isset($v['first_time']) && $v['first_time']?date('Y-m-d H:i:s',$v['first_time']):'';
            $v['second_date'] = isset($v['second_time']) && $v['second_time']?date('Y-m-d H:i:s',$v['second_time']):'';
            $v['diff_time'] = round($v['diff_t'] / 60, 2) . ' 分';
            $v['f_created_at'] = date('Y-m-d H:i:s', $v['f_created_at']);
            $v['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
        }
        return $res;
    }


    /*
     * 修改审核状态
     * $ids : 修改记录id 1,2,3,4
     * $status 审核状态  0=待审核，1=通过 2=驳回
     * $type 审核类型  1-初审 2-复审
     * $opt_id 审核人ID
     * */
    public function setContentStatus($ids, $status, $type, $opt_id)

    {
        $data['id'] = $ids;
        $data['status'] = $status;
        $data['type'] = $type;
        $data['opt_id'] = $opt_id;
        return curl_request(getKkaudioApiUrl() . '/inner/content/check/audit', $data, 'json', 'POST');
    }


    public function getAdminUser()
    {
        return $this->objToArray(DB::table('users')->get());
    }

    /**获取音乐列表
     * @param array $input
     * @return array
     */
    public function getMusic($input = []){
        $limit = $input['limit'] ?: 10;
        $page = $input['page'] ?: 1;
        $where = $input['status'] ?? 0;
        if (isset($input['uuid'])) $where .= " AND room_uid = ".$input['uuid'];
        $sql = "select m.* ,u.uuid ,u.nickname from zb_music m left join zb_user u on m.user_id = u.user_id
            WHERE status = $where order by created_time desc ";
        $data = DB::connection('api')->select($sql);
        $total = count($data);
        $data = collect($data)->forPage($page, $limit)->values()->toArray();
        return ['total' => $total, 'data' => $data, 'current_page' => intval($page)];
    }

    /**音乐审核
     * @param array $input
     * @return mixed
     */
    public function setMusicStatus($input = []){
        return curl_request(getKkaudioApiUrl() . '/inner/music/audit', $input, 'json', 'POST');
    }

    public function getFirstById($id){
        $res =  DB::connection($this->connection)->table($this->table)->find($id);
        return $this->objToArray($res);
    }

}
